package org.antstudio.autocode;

import freemarker.template.*;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

/**
 * @author:Gavin
 * @date 11/19/2014
 */
public class AutoCode {

    public static void main(String[] args) throws IOException, TemplateException {
        long start = System.currentTimeMillis();
        String baseDir = "D://template";
        System.out.println("开始生成文件");
        process(baseDir);
        long end = System.currentTimeMillis();
        System.out.println("文件生成结束,耗时："+(end-start)+"ms\n文件路径为："+baseDir+"/dist/");
    }


    private static void process(String baseDir) throws IOException, TemplateException {
        File dir = new File(baseDir);
        Configuration cfg = new Configuration(Configuration.VERSION_2_3_21);
        cfg.setDirectoryForTemplateLoading(dir);
        cfg.setDefaultEncoding("UTF-8");
        cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);

        Map<String,String> data = new HashMap<String, String>();
        data.put("name","xxx");//实体名字
        data.put("package","xxx");//包名
        data.put("menu","xxx");//菜单名字
        data.put("table","xxx");//表名

        File dist = new File(dir,"dist");
        if(!dist.exists()){
            dist.mkdir();
        }

        processAction(cfg,data,dist);

        processService(cfg,data,dist);

        processRepository(cfg,data,dist);

        processMapper(cfg,data,dist);

        processJs(cfg,data,dist);
    }
    private static void processAction(Configuration cfg,Map<String,String> data,File distDir) throws IOException, TemplateException {
        System.out.println("正在生成Action...");
        File actionDir = new File(distDir,"action");
        if(!actionDir.exists()){
            actionDir.mkdir();
        }
        Template action = cfg.getTemplate("action.ftl");
        Writer out = new OutputStreamWriter(new FileOutputStream(new File(actionDir,data.get("name")+"Action.java")));
        action.process(data,out);
        out.close();
    }

    private static void processService(Configuration cfg,Map<String,String> data,File distDir) throws IOException, TemplateException {
        System.out.println("正在生成Service...");
        File serviceDir = new File(distDir,"service");
        if(!serviceDir.exists()){
            serviceDir.mkdir();
        }
        File serviceImplDir = new File(serviceDir,"impl");
        if(!serviceImplDir.exists()){
            serviceImplDir.mkdir();
        }

        Template service = cfg.getTemplate("service.ftl");
        Writer out = new OutputStreamWriter(new FileOutputStream(new File(serviceDir,data.get("name")+"Service.java")));
        service.process(data,out);
        out.close();

        Template serviceImpl = cfg.getTemplate("serviceImpl.ftl");
        Writer implOut = new OutputStreamWriter(new FileOutputStream(new File(serviceImplDir,data.get("name")+"ServiceImpl.java")));
        serviceImpl.process(data,implOut);
        implOut.close();
    }

    private static void processRepository(Configuration cfg,Map<String,String> data,File distDir) throws IOException, TemplateException {
        System.out.println("正在生成Repository...");
        File repositoryDir = new File(distDir,"repository");
        if(!repositoryDir.exists()){
            repositoryDir.mkdir();
        }
        Template repository = cfg.getTemplate("repository.ftl");
        Writer out = new OutputStreamWriter(new FileOutputStream(new File(repositoryDir,data.get("name")+"Repository.java")));
        repository.process(data, out);
        out.close();
    }

    private static void processMapper(Configuration cfg,Map<String,String> data,File distDir) throws IOException, TemplateException {
        System.out.println("正在生成Mapper...");
        File mapperDir = new File(distDir,"mapper");
        if(!mapperDir.exists()){
            mapperDir.mkdir();
        }
        Template repository = cfg.getTemplate("mapper.ftl");
        Writer out = new OutputStreamWriter(new FileOutputStream(new File(mapperDir,data.get("name")+"Mapper.xml")));
        repository.process(data, out);
        out.close();
    }

    private static void processJs(Configuration cfg,Map<String,String> data,File distDir) throws IOException, TemplateException {
        System.out.println("正在生成Js...");
        File mapperDir = new File(distDir,"js");
        if(!mapperDir.exists()){
            mapperDir.mkdir();
        }
        Template repository = cfg.getTemplate("js.ftl");
        String name = data.get("name");
        name = name.substring(0,1).toLowerCase()+name.substring(1);
        Writer out = new OutputStreamWriter(new FileOutputStream(new File(mapperDir,name+".js")));
        repository.process(data, out);
        out.close();
    }
}
